<?php

namespace tmcore\services\crud\enum;

use app\common\enum\FileEnum;

/**
 * 搜索方式枚举
 */
class SearchEnum
{
    // 等于
    const SEARCH_EQ = '=';
    // 不等于
    const SEARCH_NEQ = '<>';
    // 大于
    const SEARCH_GT = '>';
    // 大于等于
    const SEARCH_GTEQ = '>=';
    // 小于
    const SEARCH_LT = '<=';
    // 小于等于
    const SEARCH_LTEQ = '<=';
    // IN查询
    const SEARCH_IN = 'in';
    // 模糊查询
    const SEARCH_LIKE = 'like';
    // 区间查询-兼容时间查询
    const SEARCH_BETWEEN = 'between';

    // 搜索类型
    const SEARCH_TYPE = [
        self::SEARCH_EQ,
        self::SEARCH_NEQ,
        self::SEARCH_GT,
        self::SEARCH_GTEQ,
        self::SEARCH_LT,
        self::SEARCH_LTEQ,
        self::SEARCH_IN,
        self::SEARCH_LIKE,
        self::SEARCH_BETWEEN,
    ];

    // 支持表单类型
    const ACCEPT_FORM_TYPE = [
        FormTypeEnum::RADIO,
        FormTypeEnum::SELECT,
        FormTypeEnum::INPUT,
        FormTypeEnum::DATE_PICKER
    ];

    /**
     * 获取转换后的查询类型
     * @param $search_type
     * @param $view_type
     * @param $extra
     * @return string|string[]
     */
    public static function getUseType($search_type, $view_type, $extra)
    {
        if($search_type == self::SEARCH_LIKE) {
            $use_type = '%like%';
        } elseif ($search_type == self::SEARCH_BETWEEN && $view_type == FormTypeEnum::DATE_PICKER) {
            $use_type = 'datetime';
        }  elseif ($search_type == self::SEARCH_EQ && $view_type == FormTypeEnum::CHECKBOX) {
            $use_type = 'find_in_set';
        }  elseif ($search_type == self::SEARCH_EQ && $view_type == FormTypeEnum::SELECT && !empty($extra['multiple'])) {
            $use_type = 'find_in_set';
        } else {
            $use_type = $search_type;
        }

        return $use_type;
    }
}
